//
//  CZMainViewController.swift
//  swiftsinaweibo
//
//  Created by 曾攀 on 16/3/20.
//  Copyright © 2016年 曾攀. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 自定义tabBar,添加加号按钮,系统的tabBar是只读的.不能直接使用=赋值,使用KVC来赋值
        self.setValue(CZMainTabBar(), forKey: "tabBar")
        
        //添加所有控制器
        addChildViewControllers()
        
        }
    
        //创建所有控制器
        func addChildViewControllers(){
        // 设置tabBar控制器的子控制器
        //首页
        let homeVC = CZHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        //发现
        let discoverVC = CZDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        //消息
        let messageVC = CZMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        //我
        let profileVC = CZProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        }
    
    
    func addChildViewController(controller: UIViewController,title:String,imageName:String) {
        //设置标题
        controller.title = title
        //设置普通图片
        controller.tabBarItem.image = UIImage(named: imageName)
        //拼接图片
        let hightedImageName = imageName + "_highlighted"
       // 设置高亮图片.默认情况下选中图片会被渲染成蓝色的.想要使用图片原来的颜色
        controller.tabBarItem.selectedImage = UIImage(named: hightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置item文字颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        //添加子控制器
        addChildViewController(UINavigationController(rootViewController: controller))
        
        
    }

}
